package com.ohuang.test_compose.page

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument

@Composable
fun NavControllerPage()
{
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "profile") {
        composable("profile") { CanvasPage() }
        composable("friends") { ImagePage() }
        composable(
            "Text?userId={userId}",
            arguments = listOf(navArgument("userId") {
                type = NavType.IntType //设置类型 不设置默认String
            })
        ) {
            val id = it.arguments?.getInt("userId")
            Column() {
                Spacer(modifier = Modifier.height(300.dp))
                Text(
                    text = "Userid = $id", modifier = Modifier
                        .fillMaxHeight()
                        .fillMaxWidth(), textAlign = TextAlign.Center
                )
            }
        }

    }
    Column() {
        var state by remember {
            mutableStateOf(true)
        }
        if (state)
        {
            LazyColumn(modifier = Modifier.height(200.dp)) {
                item {
                    Button(onClick = {
                        state = false
                    }) {
                        Text(text = "隐藏")
                    }
                }
                item {
                    Button(onClick = { navController.navigate("friends") }) {
                        Text(text = "Navigate  friends")
                    }
                }
                item {
                    Button(onClick = { navController.navigate("profile") }) {
                        Text(text = "Navigate  profile")
                    }
                }
                item {
                    Button(onClick = {

                        navController.navigate("friends") {

                            popUpTo("friends") { //若能找到route 弹出除route外的栈顶的其他栈
//                   inclusive=true //ture:移除最接近栈顶的 route
                            }

                        }
                    }) {
                        Text(text = "Navigate next friends, popUpTo(\"friends\") 弹出除friends的所有栈")
                    }
                }
                item {
                    Button(onClick = {
                        navController.navigate("friends") {
                            launchSingleTop = true
                        }
                    }) {
                        Text(text = "Navigate next friends, launchSingleTop=true")
                    }
                }
                item {
                    Button(onClick = {
                        navController.navigate("Text?userId=5") {
                        }
                    }) {
                        Text(text = "Navigate next Text, 传参")
                    }
                }
                item {
                    Button(onClick = {

                        navController.popBackStack("friends",false)  //false只移除friends的栈顶的页面  ture还会移除friends自身

                    }) {
                        Text(text = "Navigate popBackStack(friends,false)  ")
                    }
                }
                item {
                    Button(onClick = {

                        navController.popBackStack("friends",true)  //false只移除friends的栈顶的页面  ture还会移除friends自身

                    }) {
                        Text(text = "Navigate popBackStack(friends,true)  ")
                    }
                }
            }
        }
        else
        {
            Button(onClick = {
                state = true
            }) {
                Text(text = "显示")
            }
        }
    }

}